module.exports = {


  // 根据id获取策略
  async getPoliciesLogined(req) {
    const result = await req.policiesModel.getPoliciesLogined(req.auth.credentials.group);
    return result;
  },
  /**
   * 获取策略
   */
  async getPolicies(req) {
    const count = await req.policiesModel.getPoliciesCount();
    const result = await req.policiesModel.getPolicies(req.query);
    return {
      count,
      result,
    };
  },
  // 根据id获取策略
  async getPolicie(req) {
    const result = await req.policiesModel.getPolicie(req.params.id);
    return result;
  },
  // 添加策略
  async addPolicy(req, { boom }) {
    const exist = await req.policiesModel.isExist(req.payload);
    if (exist) {
      return boom.badRequest('存在相同的method、path、group的组合值');
    }
    const result = await req.policiesModel.addPolicy(req.payload);
    return result;
  },
  // 通过ID删除策略
  async deletePolicyById(req) {
    await req.policiesModel.deletePolicyById(req.params.id);
    return {};
  },
  // 根据ID更新策略
  async updatePolicy(req, { boom }) {
    const exist = await req.policiesModel.isExist(req.payload);
    if (exist) {
      return boom.badRequest('存在相同的method、path的组合值');
    }
    await req.policiesModel.updatePolicy(req.params.id, req.payload);
    return {};
  },

  // 获取所有的组
  async getGroups(req) {
    const groups = await req.policiesModel.getGroups();
    return groups;
  },
};
